package game.menu;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;

public class TextField implements Drawable{
	private double x,y;
	private String s;
	private Color c;
	private Font f;
	private Rectangle r;
	private boolean title;
	
	private Menu me;
	
	public TextField(Rectangle r, double x, double y, String s, Color c, Font f, Menu me,boolean title){
		this.x = x;
		this.y = y;
		this.s = s;
		this.c = c;
		this.f = f;
		this.me = me;
		this.r = r;
		this.title = title;
	}
	
	@Override
	public void draw(Graphics g) {
		g.setColor(c);
		g.setFont(f);
		if(title){
			r.width = (int) me.getWidth();
			Graphics2D g2 = (Graphics2D) g;
			FontRenderContext frc = g2.getFontRenderContext();
			float sw = (float) f.getStringBounds(s, frc).getWidth();
			LineMetrics lm = f.getLineMetrics(s, frc);
			float sh = lm.getAscent() + lm.getDescent();
			
			// center text in r
			float sx = (float) (r.x + (r.width - sw) / 2);
			float sy = (float) (r.y + (r.height + sh) / 2 - lm.getDescent());
			g2.drawString(s, sx, sy);
		}else g.drawString(s, (int)(x*me.getWidth()), (int)(y*me.getHeight()));
	}

}
